﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace sdd.subsystems.ui.screens
{
    class ScrNetworkSetup : IScreen
    {        
        public string ScreenName
        {
            get { return "Network Setup"; }
        }

        public display.IDisplay Display {get; set;}

        public apps.IApplication Application { get; set;}

        public ScreenManager Manager { set; get; }

        public void Initialize()
        {
            RegisterMenuItems();
        }

        public void Show()
        {
            //
        }

        public void Hide()
        {
            //
        }

        public void ProcessKeyPadEvent(display.KeypadEventArgs keyPadEventArgs)
        {
            //
        }



        private void RegisterMenuItems()
        {
            Dictionary<string, int> slIfMode = new Dictionary<string, int>();
            slIfMode.Add("Disabled", 0); slIfMode.Add("Static IP", 0); slIfMode.Add("DHCP", 0);
            SelectListMenuItem<int> selectList = new SelectListMenuItem<int>("Ethernet", slIfMode, "DHCP");
            selectList.ItemClick += selectList_ItemClick;
            Manager.Menus.Register(selectList, Manager.Menus.Menu("Network"));
            MenuItem Network = Manager.Menus.Menu("Network");
            Manager.Menus.Register(new IPAddressMenuItem("Ethernet IP", LocalIPAddress()), Network);

            Manager.Menus.Register(new MenuItem("Blah"), Manager.Menus.Menu("Network"));
            Manager.Menus.Register(new MenuItem("Bleh"), Manager.Menus.Menu("Network"));
            Manager.Menus.Register(new MenuItem("Blih"), Manager.Menus.Menu("Network"));
            Manager.Menus.Register(new MenuItem("Bloh"), Manager.Menus.Menu("Network"));
            Manager.Menus.Register(new MenuItem("Bluh"), Manager.Menus.Menu("Network"));

        }

        void selectList_ItemClick(object sender, MenuItemClickEventArgs<int> e)
        {
            
        }



        private IPAddress GetSubnetMask(IPAddress address)
        {
            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        if (address.Equals(unicastIPAddressInformation.Address))
                        {
                            return unicastIPAddressInformation.IPv4Mask;
                        }
                    }
                }
            }
            throw new ArgumentException(string.Format("Can't find subnetmask for IP address '{0}'", address));
        }

        private IPAddress LocalIPAddress()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    return ip;
                }
            }
            return null;
        }
    }
}
